package com.huijinhui.wlmtsys.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huijinhui.wlmtsys.enums.AppHttpCodeEnum;
import com.huijinhui.wlmtsys.mapper.InfoMapper;
import com.huijinhui.wlmtsys.pojo.dtos.PageDTO;
import com.huijinhui.wlmtsys.pojo.dtos.ResponseResult;
import com.huijinhui.wlmtsys.pojo.entity.PageQuery;
import com.huijinhui.wlmtsys.pojo.entity.WlmtsysInfo;
import com.huijinhui.wlmtsys.pojo.entity.WlmtsysProject;
import com.huijinhui.wlmtsys.service.FileStorageService;
import com.huijinhui.wlmtsys.service.InfoService;
import com.huijinhui.wlmtsys.utils.BeanUtils;
import com.huijinhui.wlmtsys.utils.CollUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class InfoServiceImpl extends ServiceImpl<InfoMapper, WlmtsysInfo> implements InfoService {

    @Autowired
    private InfoMapper infoMapper;

    private final FileStorageService fileStorageService;

    /**
     * 获取实验室介绍信息
     *
     * @return
     */
    @Override
    public List<WlmtsysInfo> getLabIntro() {
        List<WlmtsysInfo> list =  list();
        log.info("实验室介绍信息：{}",list);
        return list;
    }

    /**
     * 更新实验室介绍信息
     *
     * @param wlmtsysInfo
     */
    @Override
    public void updateLabInfo(WlmtsysInfo wlmtsysInfo) {
        wlmtsysInfo.setCreateTime(LocalDateTime.now());
        updateById(wlmtsysInfo);

    }

    /**
     * 添加实验室介绍信息
     *
     * @param wlmtsysInfo
     */
    @Override
    public void addLabInfo(WlmtsysInfo wlmtsysInfo) {
        if (!StrUtil.isEmpty(wlmtsysInfo.getTitle())) {
            wlmtsysInfo.setCreateTime(LocalDateTime.now());
            log.info("添加实验室介绍信息：{}",wlmtsysInfo);
            save(wlmtsysInfo);
        }
    }

    /**
     * 删除实验室介绍信息
     *
     * @param id
     */
    @Override
    public void deleteLabInfo(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("id不能为空");
        }
        if (id != null) {
            log.info("删除实验室介绍信息：{}", id);
            removeById(id);
        }
    }

    /**
     * 分页查询实验室介绍信息
     *
     * @param query
     * @return
     */
    @Override
    public PageDTO<WlmtsysInfo> queryEmployPage(PageQuery query) {
        Page<WlmtsysInfo> page = this.lambdaQuery()
                .like(query.getContent1() != null, WlmtsysInfo::getContent1, query.getContent1())
                .like(query.getContent2() != null, WlmtsysInfo::getContent2, query.getContent2())
                .like(query.getAddress() != null, WlmtsysInfo::getAddress, query.getAddress())
                .page(query.toMpPage());
        List<WlmtsysInfo> records = page.getRecords();
        if (CollUtils.isEmpty(records)) {
            return PageDTO.empty(page);
        }
        List<WlmtsysInfo> wlmtsysProjects = new ArrayList<>();
        for (WlmtsysInfo news : records) {
            WlmtsysInfo wlmtsysProject = BeanUtils.copyBean(news, WlmtsysInfo.class);
            wlmtsysProjects.add(wlmtsysProject);
        }
        return PageDTO.of(page, wlmtsysProjects);
    }

    /**
     * 上传图片
     *
     * @param multipartFile
     * @param imageId
     */
    @Override
    public ResponseResult uploadNews(MultipartFile multipartFile, Integer imageId) {
        //1.检查参数
        if(multipartFile == null || multipartFile.getSize() == 0 || imageId == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.上传图片到minIO中
        String fileName = UUID.randomUUID().toString().replace("-", "");
        //aa.jpg
        String originalFilename = multipartFile.getOriginalFilename();
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileId = null;
        try {
            fileId = fileStorageService.uploadImgFile("", fileName + postfix, multipartFile.getInputStream());
            log.info("上传图片到MinIO中，fileId:{}",fileId);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl-上传文件失败");
        }

        //3.更新数据库
        this.lambdaUpdate().eq(WlmtsysInfo::getId,imageId).set(WlmtsysInfo::getImage,fileId).update();
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * @param multipartFile
     * @param weimgId
     * @return
     */
    @Override
    public ResponseResult uploadNewsWe(MultipartFile multipartFile, Integer weimgId) {
        //1.检查参数
        if(multipartFile == null || multipartFile.getSize() == 0 || weimgId == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.上传图片到minIO中
        String fileName = UUID.randomUUID().toString().replace("-", "");
        //aa.jpg
        String originalFilename = multipartFile.getOriginalFilename();
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileId = null;
        try {
            fileId = fileStorageService.uploadImgFile("", fileName + postfix, multipartFile.getInputStream());
            log.info("上传图片到MinIO中，fileId:{}",fileId);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl-上传文件失败");
        }

        //3.更新数据库
        this.lambdaUpdate().eq(WlmtsysInfo::getId,weimgId).set(WlmtsysInfo::getWeimg,fileId).update();
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 获取实验室介绍信息
     *
     * @param id
     * @return
     */
    @Override
    public WlmtsysInfo getLabInfo(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("id不能为空");
        }
        WlmtsysInfo wlmtsysInfo = getById(id);
        if (wlmtsysInfo != null) {
            log.info("获取实验室介绍信息：{}",wlmtsysInfo);
            return wlmtsysInfo;
        }
        return wlmtsysInfo;
    }
}




